﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.Inventory.BL;
using com.vs.Shipping.BL;
using com.vs.Task.BL;

namespace com.vs.Shipping.BL
{
    public class WaveHelper
    {
        public string StartWave(Session sess, int iWaveNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, "WAV_StartWave", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum)});
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string CompleteWave(Session sess, int iWaveNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, "WAV_CompleteWave", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum)});
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }


        public string ReleaseWave(Session sess, int iWaveNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, "WAV_ReleaseWave", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum)});
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public string UpdateStatistics(Session sess, int iWaveNum)
        {
            DataHelper helper = null;
            //string stSQL = null;

            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, "WAV_UStatistics", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum)});
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (null != helper)
                    helper.Dispose();
            }
        }

        public string RunWave(Session sess, int iWaveNum, bool bAutoRelease)
        {
            WaveDO wavDO = null;
            WaveFlowHeaderDO wfhDO = null;
            List<WaveFlowDetailDO> ltFlowDetail = null;
            //string stError = null;

            try
            {
                wavDO = WaveDO.GetModelByPK(sess, iWaveNum);
                if (null == wavDO)
                    return MessageManager.MSG_WAV_001; //波次未找到

                if (wavDO.STATUS > WaveConstants.iWavePending)
                    return MessageManager.MSG_WAV_002; //波次不在可运行的状态

                if (wavDO.TOTAL_SHIPMENTS == 0 || 
                    wavDO.TOTAL_LINES == 0 || 
                    wavDO.TOTAL_QTY == 0)
                    return MessageManager.MSG_WAV_004; //空波次

                wfhDO = WaveFlowHeaderDO.GetModelByPK(sess, wavDO.WAVE_FLOW);
                if (null == wfhDO)
                    return MessageManager.MSG_WAV_005; //波次流程无效

                ltFlowDetail = WaveFlowDetailDO.GetListByColumn(sess, "WAVE_FLOW", wavDO.WAVE_FLOW);
                if (null == ltFlowDetail ||
                    ltFlowDetail.Count == 0)
                    return MessageManager.MSG_WAV_005; //波次流程无效

                WaveThread thread = new WaveThread(sess, iWaveNum, wfhDO, ltFlowDetail, bAutoRelease);
                Thread t = new Thread(new ThreadStart(thread.Process));
                t.Start();
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        public string DeleteWave(Session sess, int iWaveNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                return helper.GetString(CommandType.StoredProcedure, "WAV_DWave", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum)
                });
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (null != helper)
                    helper.Dispose();
            }
        }

        public string CancelWave(Session sess, int iWaveNum)
        {
            DataHelper helper = null;
            string stErr = null;

            try
            {
                helper = new DataHelper(sess);
                stErr = helper.GetString(CommandType.StoredProcedure, "WAV_VCancelWave", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum)
                });

                if (!StringManager.IsEmpty(stErr))
                    return stErr;

                return helper.GetString(CommandType.StoredProcedure, "WAV_CancelWave", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum)
                });
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (null != helper)
                    helper.Dispose();
            }
        }

        public DataTable GetWaveResult(Session sess, int iWaveNum)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                return helper.GetTable(CommandType.StoredProcedure, "WAV_RWaveResult", new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum)
                });
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (null != helper)
                    helper.Dispose();
            }
        }

        public string OverrideData(Session sess, int iWaveNum, string stSP)
        {
            DataHelper helper = null;
            try
            {
                helper = new DataHelper(sess);
                return helper.GetString(CommandType.StoredProcedure, stSP, new IDataParameter[]{
                        DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum)});
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
    }
}
